package com.engine4cloud.tdf.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.education.convert.EduActivitiesFileConvert;
import com.engine4cloud.tdf.education.dto.EduActivitiesFileDTO;
import com.engine4cloud.tdf.education.dto.query.EduActivitiesFileQueryDTO;
import com.engine4cloud.tdf.education.dto.result.EduActivitiesFileResultDTO;
import com.engine4cloud.tdf.education.entity.EduActivityFileEntity;
import com.engine4cloud.tdf.education.mapper.EduActivityFileMapper;
import com.engine4cloud.tdf.education.service.EduActivityFileService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 活动-文件表
 *
 * @author wenjie
 * @date 2024-08-13 11:18:36
 */
@Service
public class EduActivityFileServiceImpl extends ServiceImpl<EduActivityFileMapper, EduActivityFileEntity> implements EduActivityFileService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSave(EduActivitiesFileQueryDTO dto, List<EduActivitiesFileDTO> fileList, TdfUser user) {
        this.saveBatch(EduActivitiesFileConvert.INSTANCE.createList(dto, fileList, user));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate(EduActivitiesFileQueryDTO dto, List<EduActivitiesFileDTO> fileList, TdfUser user) {
        List<EduActivitiesFileResultDTO> existedList = this.findList(dto);
        List<EduActivityFileEntity> preSaveFileList = this.preSaveFileList(dto, fileList, user);
        if (CollectionUtils.isNotEmpty(preSaveFileList)) {
            this.saveBatch(preSaveFileList);
        }

        List<Long> preDeleteFileList = this.preDeleteFileList(existedList, fileList);
        if (CollectionUtils.isNotEmpty(preDeleteFileList)) {
            this.removeBatchByIds(preDeleteFileList);
        }
    }

    @Override
    public List<EduActivitiesFileResultDTO> findList(EduActivitiesFileQueryDTO dto) {
        List<EduActivityFileEntity> list = super.list(wrapper(dto));

        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(EduActivitiesFileConvert.INSTANCE::convert).toList();
    }


    private List<Long> preDeleteFileList(List<EduActivitiesFileResultDTO> existedList, List<EduActivitiesFileDTO> fileList) {
        if (CollectionUtils.isEmpty(existedList)) {
            return Collections.emptyList();
        }

        Map<Long, EduActivitiesFileDTO> modelFileMap = fileList.stream().filter(model -> Objects.nonNull(model.getId())).collect(Collectors.toMap(EduActivitiesFileDTO::getId, Function.identity(), (v1, v2) -> v1));
        return existedList.stream().map(EduActivitiesFileResultDTO::getId).filter(id -> !modelFileMap.containsKey(id)).toList();
    }

    private List<EduActivityFileEntity> preSaveFileList(EduActivitiesFileQueryDTO dto, List<EduActivitiesFileDTO> fileList, TdfUser user) {
        if (CollectionUtils.isEmpty(fileList)) {
            return Collections.emptyList();
        }

        return fileList.stream().filter(file -> Objects.isNull(file.getId())).map(file -> EduActivitiesFileConvert.INSTANCE.create(dto.getActivityCode(), dto.getFileType(), file, user)).toList();

    }

    private LambdaQueryWrapper<EduActivityFileEntity> wrapper(EduActivitiesFileQueryDTO query) {
        LambdaQueryWrapper<EduActivityFileEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(EduActivityFileEntity::getId, v));
        Queries.accept(query.getCode(), v -> wrapper.eq(EduActivityFileEntity::getCode, v));
        Queries.accept(query.getActivityCode(), v -> wrapper.eq(EduActivityFileEntity::getActivityCode, v));
        Queries.accept(query.getFileType(), v -> wrapper.eq(EduActivityFileEntity::getFileType, v));
        return wrapper;
    }
}